package com.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.mapper.ProjectItemPicMapper;
import com.pojo.Project;
import com.pojo.Role;
import com.pojo.UserConfirm;
import com.pojo.UserLaunch;
import com.service.ProjectService;
import com.service.TypeService;
import com.service.UserLaunchService;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private UserLaunchService userLaunchService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private ProjectItemPicMapper projectItemPicMapper;
    /**
     * 多条件查询
     * @param pageNum
     * @param projectName 项目名称
     * @param status 项目状态
     * @param typeid 项目分类
     * @param order_by  排序。。。1 时间  2 支持金额  3 支持人数
     * @return
     */
    @RequestMapping("/selectAllProjects")
    public Object selectAllProjects(@RequestParam(value = "pageNum",required = true,defaultValue= "1") int pageNum,
                                    @RequestParam(value = "projectName",required = true,defaultValue= "") String projectName,
                                    @RequestParam(value = "status",required = true,defaultValue="") String status,
                                    @RequestParam(value = "typeid",required = true,defaultValue="0") long typeid,
                                    @RequestParam(value = "order_by",required = true,defaultValue= "0") long order_by){
        R r=new R<Role>();
        Map map=new HashMap();
        map.put("pageNum",pageNum);
        map.put("projectName",projectName);
        map.put("status",status);
        map.put("typeid",typeid);
        map.put("order_by",order_by);
        PageInfo pageInfo = projectService.selectAllProjects(map);
        r.setCode(200);
        r.setData(pageInfo);
        return r;
    }

    @RequestMapping("/selectProjectByProjectName")
    public Object selectProjectByProjectName(
      @RequestParam(value = "ProjectName",required = true,defaultValue= "") String ProjectName){
        R r=new R<Role>();
        Project project= projectService.selectProjectByProjectName(ProjectName);
        r.setCode(200);
        r.setData(project);
        return r;
    }

    /**
     * 插入项目信息
     * @param project
     * @return 返回id
     */
    @RequestMapping("/insertProject")
    public Object insertProject(Project project){
        R r=new R();
        int i = projectService.insertProject(project);
        if(i!=0){
            r.setCode(200);
            r.setMessage("插入成功");
            r.setData(i);
        }else{
            r.setCode(400);
            r.setMessage("插入失败");
            r.setData(0);
        }
        return r;
    }

    /**
     * 只能更新项目部分信息，结合实际情况完成项目信息的更新
     * @param project
     * @return
     */
    @RequestMapping("/updateProject")
    public Object updateProject(Project project){
        int i = projectService.updateProject(project);
        R r=new R();
        if(i!=0){
            r.setCode(200);
            r.setMessage("更新成功");
            r.setData(i);
        }else{
            r.setCode(400);
            r.setMessage("更新失败");
            r.setData(0);
        }
        return r;
    }
    //更新project
    @RequestMapping("/updateProjectUserJson")
    public Object updateProjectUserJson(HttpServletRequest request){

        String project1 = request.getParameter("project");
        Project project = JSONArray.parseObject(project1, Project.class);
        int i = projectService.updataProjectEnd(project);
        R r=new R();
        if(i!=0){
            r.setCode(200);
            r.setMessage("更新成功");
            r.setData(i);
        }else{
            r.setCode(400);
            r.setMessage("更新失败");
            r.setData(0);
        }
        return r;
    }

    /**
     * 删除项目，项目已通过审核，正在进行时不建议进行删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteProject")
    public Object deleteProject(long id){
        int i = projectService.deleteProject(id);
        R r=new R();
        if(i!=0){
            r.setCode(200);
            r.setMessage("删除成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("删除失败");
            r.setData(0);
        }
        return r;
    }

    /**
     * 根据id查询项目
     * @param id
     * @return
     */
    @RequestMapping("/selectProjectById")
    public Object selectProjectById(long id){
        Project project = projectService.selectProjectById(id);
        R r=new R();
        if(project!=null){
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(project);
        }else{
            r.setCode(400);
            r.setMessage("查询失败");
            r.setData(0);
        }
        return r;
    }

    /**
     * 根绝用户id来查询该用户发布的所有项目，根据status来选择项目状态
     * @param pageNum
     * @param userId
     * @param status
     * @return
     */
    @RequestMapping("/selectProjectByuserId")
    public Object selectProjectByuserId(@RequestParam(defaultValue = "1")String pageNum,long userId,
                                        @RequestParam(defaultValue = "")String status){
        PageInfo<Project> pageInfo = projectService.selectProjectByuserId(Integer.parseInt(pageNum), userId, status);
        return pageInfo;
    }

    /**
     * 发布项目 待完善
     * @param request
     * @return
     */
    @RequestMapping("/stratProject")
    public Object startProject(HttpServletRequest request){
        //获取项目详情图
        String itemPicPath = request.getParameter("itemPicPath");
        //获取发起人信息
        String userlaunchjson = request.getParameter("userlaunchjson");
        UserLaunch userLaunch = JSONArray.parseObject(userlaunchjson, UserLaunch.class);
        //获取项目信息
        String projectjson = request.getParameter("projectjson");
        Project project = JSONArray.parseObject(projectjson, Project.class);
        project.setUserid(userLaunch.getUserid());
        //获取项目类型
        String types = request.getParameter("types");
        String[] split = types.split(",");
        //System.out.println(split.length);
        //首先所有数据获取到  然后根据项目id来判断是否是新进入项目发布，还是通过返回上一步来进入项目的
        String projectID = request.getParameter("projectID");
        String userLaunchID = request.getParameter("userLaunchID");
        //不等于则是第n次进入这一步，只需对项目进行更新即可
        if (!projectID.equals("null")){
            project.setId(Long.parseLong(projectID));
            userLaunch.setId(Long.parseLong(userLaunchID));
            int i = projectService.updateStartProject(project);
            userLaunch.setUserid(project.getId());
            int i1 = userLaunchService.updateUserLaunch(userLaunch);
            int i2 = typeService.deleteTypeByProjectId(Long.parseLong(projectID));
            projectItemPicMapper.update(project.getId(),itemPicPath);
            for (int j = 0; j < split.length; j++) {
                typeService.insertProjectType(Long.parseLong(projectID),split[j]);
            }
        }else {
            //将项目加入到数据库
            int i = projectService.startProject(project);
            userLaunch.setUserid(project.getId());
            //将发起人信息存入数据库
            int i1 = userLaunchService.addUserLaunch(userLaunch);
            //将分类信息传入数据库
            projectItemPicMapper.insert(project.getId(),itemPicPath);
            for (int j = 0; j < split.length; j++) {
                typeService.insertProjectType(project.getId(),split[j]);
            }
            projectID = project.getId()+"";
            userLaunchID = userLaunch.getId()+"";
        }
        //将id封装到数组中返回给前端保存
        String[] PUI={projectID,userLaunchID};
        return PUI;
    }

    /**
     * 将项目支付信息存入数据库
     * @param userConfirm
     * @return
     */
    @RequestMapping("/insertUserConrirm")
    public Object insertUserConrirm(UserConfirm userConfirm){
        int i = userLaunchService.insertUserConfirm(userConfirm);
        System.out.println(userConfirm);
       return userConfirm.getId();
    }
    @RequestMapping("/selectProjects")
    public List<Project> selectProjects(){
        return projectService.selectProjects();
    }

    @RequestMapping("/selectUserLaunchById")
    public Object selectUserLaunchByUserId(long userid){
        UserLaunch userLaunches = userLaunchService.selectUserLaunchByUserId(userid);
        return userLaunches;
    }

}
